package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IProfitHistoryService;
import com.ruoyi.system.service.ISignHistoryService;
import com.ruoyi.system.service.ISignRuleService;
import com.ruoyi.system.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 签到模块Controller
 * 
 * @author sjt
 * @date 2021-11-30
 */
@RestController
@RequestMapping("/SignModular")
public class SignModularController extends BaseController
{
    @Autowired
    private ISignHistoryService signHistoryService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISignRuleService signRuleService;

    @Autowired
    private IProfitHistoryService profitHistoryService;

    /**
     * 获取签到历史详细信息
     */
    @GetMapping("/getSign")
    public AjaxResult getSign(Long userId)
    {
        if(userId==null){
            return AjaxResult.error("未传入用户编号");
        }else {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(userId);
            List<UserInfo> userlist = userInfoService.selectUserInfoList(userInfo);
            if(userlist.size()!=1){
                return AjaxResult.error("用户不存在/非法");
            }
        }
        SignHistory signHistory = new SignHistory();
        signHistory.setUserId(userId);
        List<SignHistory> list = signHistoryService.selectSignHistoryList(signHistory);
        if(list.size()==0){
            signHistory.setSignTimes(0l);
            signHistory.setSignType("0");
            signHistoryService.insertSignHistory(signHistory);
        }
        SignHistory signHistoryout = signHistoryService.selectSignHistoryByUserId(userId);
        signHistoryout.setSignRuleList(signRuleService.selectSignRuleList(new SignRule()));
        return AjaxResult.success(signHistoryout);
    }

    /**
     * 新增签到历史
     */
    @PostMapping("/loginSignIn")
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public AjaxResult loginSignIn(@RequestBody SignHistory signHistory)
    {
        if(signHistory.getUserId()==null){
            return AjaxResult.error("未传入用户编号");
        }else {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(signHistory.getUserId());
            List<UserInfo> userlist = userInfoService.selectUserInfoList(userInfo);
            if(userlist.size()!=1){
                return AjaxResult.error("用户不存在/非法");
            }
        }
        List<SignHistory> list = signHistoryService.selectSignHistoryList(signHistory);
        List<SignRule> SignRuleList = signRuleService.selectSignRuleList(new SignRule());
        UserInfo userInfo = userInfoService.selectUserInfoByUserId(signHistory.getUserId());

        ProfitHistory profitHistory = new ProfitHistory();
        profitHistory.setGetUserId(signHistory.getUserId());
        profitHistory.setGiftTime(new Date());

        if(list.size()>0){
            if(list.get(0).getSignType().equals("1")){
                return AjaxResult.error("今日已签到");
            }
            list.get(0).setSignType("1");
            list.get(0).setSignTimes(list.get(0).getSignTimes()+1);
            //获取签到奖励
            Long prise = SignRuleList.get(new Long(list.get(0).getSignTimes()-1).intValue()).getSignReward();
            //修改用户余额
            userInfo.setCoin(userInfo.getCoin()+prise);
            userInfoService.updateUserInfo(userInfo);
            //新增用户收益记录
            profitHistory.setMemo(prise.toString());
            profitHistoryService.insertProfitHistory(profitHistory);
            //新增签到记录
            signHistoryService.updateSignHistory(list.get(0));
        }else{
            signHistory.setSignTimes(1l);
            signHistory.setSignType("1");
            //获取签到奖励
            Long prise = SignRuleList.get(0).getSignReward();
            //修改用户余额
            userInfo.setCoin(userInfo.getCoin()+prise);
            userInfoService.updateUserInfo(userInfo);
            //新增用户收益记录
            profitHistory.setMemo(prise.toString());
            profitHistoryService.insertProfitHistory(profitHistory);
            //新增签到记录
            signHistoryService.insertSignHistory(signHistory);
        }
        return AjaxResult.success("签到成功");
    }
}
